// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'

import Vuet from 'vuet'
import VuetScroll from 'vuet-scroll'

import VueResource from 'vue-resource'
import ElementUI from 'element-ui'
import VueQuillEditor from 'vue-quill-editor'
import 'element-ui/lib/theme-chalk/index.css'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import Index from './index.vue'
import VueClipboard from 'vue-clipboard2'

import router from '../../router'

import ajax from '@U/ajax'

import verify from "vue-verify-plugin"
import VueAMap from 'vue-amap'
import verifyRules from '@common/verifyRules.js'

import xySave from '@C/xySave'

import utils from '@U/utils'

import user from "@M/user";

import xyComponents from '@C'

import Vtip from 'vtip'

import filter from '@M/filter'
import directive from '@M/directive'

import "vtip/lib/index.min.css"

import echarts from 'echarts'

require("echarts/map/js/china");


filter.init(Vue);
directive.init(Vue);

Vue.config.productionTip = false
Vue.use(ElementUI)
Vue.use(VueQuillEditor)
Vue.use(VueResource);
Vue.use(VueClipboard)
Vue.use(ajax);
Vue.use(utils);
Vue.use(xyComponents);
Vue.use(VueAMap); //高德地图
Vue.use(verify, {
  rules: verifyRules,
  blur: true
});
Vue.use(user)

Vue.use(Vtip.directive)

Vue.use(Vuet)
Vue.use(VuetScroll)

Vue.component('xySave', xySave);

var eventHandle = new Vue();

// 初始化vue-amap
VueAMap.initAMapApiLoader({
  // 高德的key
  key: '1cd8eb70f2c40c65bc8a4bca523e5961',
  // 插件集合
  plugin: [
    'AMap.Autocomplete',
    'AMap.PlaceSearch',
    'AMap.Scale',
    'AMap.OverView',
    'AMap.ToolBar',
    'AMap.MapType',
    'AMap.PolyEditor',
    'AMap.CircleEditor'
  ],
  // 高德 sdk 版本，默认为 1.4.4
  v: '1.4.4'
});

router.beforeEach((to, from, next) => {
  var toPath = to.path;
  if (/\.html$/.test(toPath)) {
    toPath = toPath.replace(/\.html$/, "");
    next({path: toPath, query: to.query});
  } else {
    next();
  }

});

router.afterEach((to) => {
  eventHandle.$emit("ACTIVE_MENU", to);
});

Vue.mixin({
  activated(){
    //xy-table 重新排版
    this.$root.eventHandle.$emit('TABLE_LAYOUT');
  },
});

// 全局插件
Vue.use({
  install(Vue) {
    Vue.prototype.$echarts = echarts

    //添加全局样式
    Vue.prototype.inputClass = {
      input_text: true
    }
    //添加全局样式
    Vue.prototype.classObject = {
      input_text: true
    }
    
    // 判断直播场景
    Vue.prototype.sceneType = function(item) {
      if(item=='alone'){
        // return '视频'
        return '一般'
      }
    }
    Vue.prototype.liveType = function(item) {
      if(item=='视频'){
        return 'alone'
      }
    }

    // 直播状态
    Vue.prototype.statusType = function(item) {
      if(item =='END'){
        return '直播结束'
      }else if(item == 'LIVE'){
        return '直播中'
      }else if(item == 'WAITING'){
        return '未开始'
      }else{
        return '回放中'
      }
    }

    // 转换布尔值
    Vue.prototype.booleanChange = function(item) {
      if(item =='N'){
        return false
      }else{
        return true
      }
    }
    // 转换Y
    Vue.prototype.letterChange = function(item) {
      if(item == false){
        return 'N'
      }else{
        return 'Y'
      }
    }
  
    // 判断功能开关
    Vue.prototype.switchVals = function(item) {
      if(item=='Y' || item==true){
        return 1
      }else{
        return 0
      }
    }

    //alert弹窗
    Vue.prototype.$alert = function(opts, cb) {
      opts = opts || {};
      if (typeof opts == "string") {
        opts = {
          content: opts
        };
      }
      if (typeof cb == "function") {
        opts.callback = cb;
      }
      eventHandle.$emit("ALERT", opts);
    }

    //confirm弹窗
    Vue.prototype.$xyConfirm = function(opts, cb1, cb2) {
      opts = opts || {};
      if (typeof opts == "string") {
        opts = {
          content: opts
        };
      }
      opts.btns = opts.btns || [];
      opts.btns[0] = opts.btns[0] || {
        text: "确 定",
        type: "primary",
        callback: cb1
      };

      opts.btns[1] = opts.btns[1] || {
        text: "取 消",
        type: "",
        callback: cb2
      };

      eventHandle.$emit("CONFIRM", opts);
    }

    Vue.prototype.$hideDialog = function() {
      eventHandle.$emit("HIDE_DIALOG");
    }

    Vue.prototype.$filterNumber = function(n,field,o){
      n = n || '';
      n+='';
      n = n.replace(/[^0-9]/g,'');

      this.$nextTick(()=>{
        this.$set(o || this.submitSignData,field,n);
      });
    }

    Vue.prototype.$handleTip = function(v){
      v = v || '';
      return v.replace(/([\s\S]{8})/g,"$1<br>");
    };

    /**
     * 数据对比
     * 以提交的数据为依据
     * **/
    Vue.prototype.compareData = function(newData, oldData) {
      // console.log('比较测试数据');
      if (!newData || !oldData)
        return !newData && !oldData
          ? false
          : true;

      //调换顺序防止string 和int类型的对比
      var type = typeof(newData)
      if (type == 'number' || type == 'string' || type == 'boolean')
        return newData != oldData;

      if (newData.constructor != oldData.constructor)
        return true

        //日期的对比
      if (newData.constructor == Date)
        return newData.getTime() != oldData.getTime()
        // 对象对比
      if (newData.constructor == Object) {
        var data1 = newData,
          data2 = oldData;
        if (Object.keys(newData).length < Object.keys(oldData).length) {
          data1 = oldData
          data2 = newData
        }
        for (var key in data1) {
          if (this.compareData(data1[key], data2[key]))
            return true
        }
      }
      //数组对比
      if (newData.constructor == Array) {
        if (newData.length != oldData.length)
          return true
        for (var i = 0; i < newData.length; i++) {
          if (this.compareData(newData[i], oldData[i]))
            return true
        }
      }
      return false
    }

    Vue.prototype.$toPage = function(val, obj) {
      let self = this;
      if (typeof val == "object") {
        obj = val;
        val = self.$router.path;
      }
      setTimeout(function() {
        self.$router.push({
          path: val,
          query: obj || {}
        })
      }, 0);
    }

    Vue.prototype.$close = function(noJump){
      eventHandle.$emit("CLOSE_CURRENT_TAB",noJump);
    }

    Vue.prototype.$removeVerifyClass = function(e){
      console.log(e);
    }

    Vue.prototype.$upImgload = function(file,size) {
      var isImg = /^image\/(jpeg|png|jpg|gif)$/.test(file.type);
      const isLt2M = file.size / 1024 / 1024 <= size; //图片大小不超过sizeMB
      if (!isImg) {
        this.$message.error("上传图片格式不对!");
        return
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 "+size+"M!");
        return
      }
      return true
    }

    /* ajax请求 */
    //Vue.prototype.ajax = ajax

    /* 每页请求条数 */

    Vue.prototype.pageSize = 50;

    Vue.prototype.$merge = function(obj1, obj2) {
      obj1 = obj1 || {};
      obj2 = obj2 || {};
      var obj1 = JSON.parse(JSON.stringify(obj1));
      var obj2 = JSON.parse(JSON.stringify(obj2));
      for (var key in obj2) {
        obj1[key] = obj2[key];
      }
      return obj1;
    }

  }

})

const vuet = new Vuet({
  modules: {
    xyOrg: {
      data() {
        return {}
      }
    }
  }
})


/* eslint-disable no-new */
var vue = new Vue({
  el: '#app',
  router,
  vuet,
  template: '<Index/>',
  components: {
    Index
  },
  data: {
    /* 	用于创建页签事件派发
    	派发事件：this.$root.eventHandle.$emit('事件名','传递的参数')
  		事件接收:
  		created:function(){
			var self = this;
  			this.$root.eventHandle.$on('事件名',function(d){

  			})
		}
	  */
    eventHandle: eventHandle
  },
  methods: {}
});
